package com.itheima.util;

import com.baidu.aip.imageclassify.AipImageClassify;
import com.baidu.aip.nlp.AipNlp;
import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;

public class AiUtils {

    public static String img(MultipartFile file) throws IOException {
        //设置APPID/AK/SK
        String APP_ID = "88799953";
        String API_KEY = "kL2Q8TxVDM6Lh426WukyWZyK";
        String SECRET_KEY = "BWCnDLpBY5JW6y9j7BTVyoPw3hSwhvpJ";
        // 初始化一个AipImageClassify
        AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);

        // 调用接口
        JSONObject res = client.advancedGeneral(file.getBytes(), new HashMap<String, String>());
        JSONObject result = res.getJSONArray("result").getJSONObject(0);

        String s = "这张图" + result.getDouble("score")*100 + "%的可能是" + result.getString("root") + "-" +result.getString("keyword");
        return s;
    }

//    public static String word(MultipartFile file) throws IOException {
//        //设置APPID/AK/SK
//        String APP_ID = "改成你的APPID";
//        String API_KEY = "改成你的KEY";
//        String SECRET_KEY = "改成你的SECRET_KEY";
//        // 初始化一个AipOcr
//        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
//        // 调用接口
//        JSONObject res = client.basicGeneral(file.getBytes(), new HashMap<String, String>());
//        return res.toString(2);
//    }

    public static String nlp(String text){
        //设置APPID/AK/SK
        String APP_ID = "88791833";
        String API_KEY = "8qHruOvu19jHRA2w8ca3kicg";
        String SECRET_KEY = "6carD86xg02HwZ8CGjSL9ISZw6DlFtOd";
        // 初始化一个AipNlp
        AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);

        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();

        // 情感倾向分
        JSONObject res = client.sentimentClassify(text, options);
        return res.toString(2);
    }

//    public static void main(String[] args) {
//        String text = "我爱你";
//        System.out.println(nlp(text));
//    }
}